﻿using System;
using System.IO;
using System.Reflection;

namespace Fast.Reflection
{
    /// <summary>
    /// 程序集帮助类
    /// </summary>
    public class AssemblyHelper
    {
        /// <summary>
        /// 根据路径加载程序集
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Assembly LoadAssembly(string path)
        {
            if (!File.Exists(path)) return default;
            return Assembly.LoadFrom(path);
        }
        /// <summary>
        /// 获取程序集名称
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static string GetAssemblyName(Assembly assembly)
        {
            return assembly.GetName().Name;
        }

        /// <summary>
        /// 获取程序集名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetAssemblyName(Type type)
        {
            return GetAssemblyName(type.GetTypeInfo());
        }

        /// <summary>
        /// 获取程序集名称
        /// </summary>
        /// <param name="typeInfo"></param>
        /// <returns></returns>
        public static string GetAssemblyName(TypeInfo typeInfo)
        {
            return GetAssemblyName(typeInfo.Assembly);
        }

        /// <summary>
        /// 获取启动程序集名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetEntryAssemblyName()
        {
            return GetAssemblyName(Assembly.GetEntryAssembly());
        }
    }
}
